package br.ufrgs.meetapp.utils;

import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import br.ufrgs.meetapp.activities.R;

public class ActivityIndicator {

	Context context;
	
	PopupWindow popupActivityIndicator;
	
	public ActivityIndicator(Context _context) {
		context = _context;
	}
	
	/***
	 * Show the activity indicator popup (should be called whenever there is <br>
	 * the need to make the user wait)
	 */
	public void show() {
		try {

			Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
					.getDefaultDisplay();
			int width = display.getWidth();
			int height = display.getHeight();

			// We need to get the instance of the LayoutInflater, use the
			// context of this activity
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			// Inflate the view from a predefined XML layout
			
			View layout = inflater.inflate(R.layout.activity_indicator, null);
			View root =  layout.findViewById(R.id.popup_element);
			
			popupActivityIndicator = new PopupWindow(root, width, height, true);			
			popupActivityIndicator.showAtLocation(layout, Gravity.CENTER, 0, 0);

		} catch (Exception e) {
			System.out
					.println("Error in ActivityIndicator.showActivityIndicator - trying to show activity indicator");
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	/***
	 * Dismiss the activity indicator when we want to let the user interact <br>
	 * with the app
	 */
	public void dismiss() {
		try {
			if (popupActivityIndicator != null)
				popupActivityIndicator.dismiss();
		} catch (Exception exception) {
			System.out
					.println("Error in ActivityIndicator.dismissActivityIndicator - trying to dismiss the popup");
			System.out.println(exception.getMessage());
			exception.printStackTrace();
		}

	}

}
